IncludeFile "Common.pb"

Structure WindowXY
  X.i
  Y.i
EndStructure

Global LoadingWindowXY.WindowXY

Procedure ProcessBooleanOption(Gadget,OptionString.s,Def.i,IsLoad.i,LoadString.s)
  Select IsLoad
  Case 0
    If(FindString(LoadString,OptionString,0)>0)
  	  SetGadgetState(Gadget,Val(RemoveString(LoadString,OptionString)))
    EndIf
  Case 1
    WriteStringN(0,OptionString+Str(GetGadgetState(Gadget)))
  Case 2
    SetGadgetState(Gadget,Def)
  EndSelect
EndProcedure

Procedure ProcessComboOption(Gadget,OptionString.s,Def.i,IsLoad.i,LoadString.s)
  Select IsLoad
  Case 0
    If(FindString(LoadString,OptionString,0)>0)
  	  GVal = Val(RemoveString(LoadString,OptionString))
 	  	For i = 0 To CountGadgetItems(Gadget)-1 
    		If GetGadgetItemData(Gadget,i) = GVal
			    SetGadgetState(Gadget,i)
			    Break
		    EndIf
	    Next
    EndIf
  Case 1
    WriteStringN(0,OptionString+Str(GetGadgetItemData(Gadget,GetGadgetState(Gadget))))
  Case 2
    SetGadgetState(Gadget,Def)
  EndSelect
EndProcedure

Procedure ProcessComboOptionWindowXY(Gadget,OptionString1.s,OptionString2.s,Def,IsLoad.i,LoadString.s)
  Select IsLoad
  Case 0
    If(FindString(LoadString,OptionString1,0)>0)
  	  LoadingWindowXY\X = Val(RemoveString(LoadString,OptionString1))
    EndIf
    If(FindString(LoadString,OptionString2,0)>0)
  	  LoadingWindowXY\Y = Val(RemoveString(LoadString,OptionString2))
    EndIf
    If((LoadingWindowXY\X <> -1) And (LoadingWindowXY\Y <> -1))
 	  	For i = 0 To CountGadgetItems(Gadget)-1 
        *Temp.WindowXY = GetGadgetItemData(Gadget,i)
    		If((*Temp\X=LoadingWindowXY\X) And (*Temp\Y=LoadingWindowXY\Y))
			    SetGadgetState(Gadget,i)
			    Break
		    EndIf
	    Next
    EndIf
  Case 1
    *Temp.WindowXY = GetGadgetItemData(Gadget,GetGadgetState(Gadget))
    WriteStringN(0,OptionString1+Str(*Temp\X))
    WriteStringN(0,OptionString2+Str(*Temp\Y))
  Case 2
    SetGadgetState(Gadget,Def)
  EndSelect
EndProcedure

Procedure ProcessOptions(IsLoad.i,LoadString.s = "")
  If(IsLoad = 1)
    WriteStringN(0,"[s32]")
  EndIf
  
  ProcessComboOptionWindowXY(#ScreenSize,"Window_Width=","Window_Height=",0,IsLoad,LoadString)
  ProcessComboOption(#OpenGLGLSLVersion,"OpenGL_Support_GLSL_Version=",0,IsLoad,LoadString)

  If(IsLoad = 1)
    WriteStringN(0,"[s8]")
  EndIf
  
  ProcessBooleanOption(#VSync,"Window_VSync=",0,IsLoad,LoadString)
  ProcessComboOption(#ScreenDepth,"Window_Depth=",0,IsLoad,LoadString)
  ProcessBooleanOption(#Fullscreen,"Window_Fullscreen=",0,IsLoad,LoadString)
  ProcessComboOption(#Hertz,"Window_Hertz=",0,IsLoad,LoadString)
  ProcessComboOption(#MSAA,"Window_MSAA=",0,IsLoad,LoadString)

  ProcessBooleanOption(#DeferredShading,"SceneManager_UseDSCompositor=",0,IsLoad,LoadString)
  ProcessBooleanOption(#DeferredMSAA,"SceneManager_DSCompositor_AA=",0,IsLoad,LoadString)
  ProcessComboOption(#DeferredMSAALevel,"SceneManager_DSCompositor_AA_Samples=",0,IsLoad,LoadString)

  ProcessComboOption(#OpenGLMode,"OpenGL_Driver_Mode=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLForward,"OpenGL_ForwardContext=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLVbo,"OpenGL_Support_VBO=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLVsync,"OpenGL_Support_VSync=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLDrawRange,"OpenGL_Support_DrawRange=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLMultitextures,"OpenGL_Support_MultiTextures=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLAatextures,"OpenGL_Support_AATextures=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLS3tc,"OpenGL_Support_S3TC=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLFbo,"OpenGL_Support_FBO=",0,IsLoad,LoadString)
  ProcessBooleanOption(#OpenGLGlsl,"OpenGL_Support_GLSL=",0,IsLoad,LoadString)
EndProcedure

Procedure Load()
	If ReadFile(0, "Launcher.ini")
		While Eof(0) = 0
			InSt.s = ReadString(0)
			ProcessOptions(0,InSt)
		Wend
		CloseFile(0)
	Else
		MessageRequester("Information","Couldn't open the file")
	EndIf
EndProcedure

Procedure Save()
	If CreateFile(0,"Launcher.ini")
		ProcessOptions(1,"")
		CloseFile(0)
	Else
		MessageRequester("Information","Couldn't create the file")
	EndIf

EndProcedure

Procedure AddComboValue(Gadget,Text.s,Value)
  AddGadgetItem(Gadget,-1,Text)
  SetGadgetItemData(Gadget,CountGadgetItems(Gadget)-1,Value)
EndProcedure

Procedure AddWXYValue(Gadget,Text.s,X.i,Y.i)
  *Temp.WindowXY = AllocateMemory(SizeOf(WindowXY))
  *Temp\X = X
  *Temp\Y = Y
  AddComboValue(Gadget,Text,*Temp)
EndProcedure


Open_Window_0();

AddWXYValue(#ScreenSize,"1600*1200",1600,1200)
AddWXYValue(#ScreenSize,"1280*1024",1280,1024)
AddWXYValue(#ScreenSize,"1024*768",1024,768)
AddWXYValue(#ScreenSize,"800*600",800,600)
AddWXYValue(#ScreenSize,"640*480",640,480)

AddComboValue(#ScreenDepth,"Default",0)
AddComboValue(#ScreenDepth,"16",16)
AddComboValue(#ScreenDepth,"32",32)

AddComboValue(#MSAA,"Off",0)
AddComboValue(#MSAA,"2",2)
AddComboValue(#MSAA,"4",4)
AddComboValue(#MSAA,"6",6)
AddComboValue(#MSAA,"8",8)
AddComboValue(#MSAA,"16",16)

AddComboValue(#Hertz,"Default",0)
AddComboValue(#Hertz,"60",60)
AddComboValue(#Hertz,"75",75)
AddComboValue(#Hertz,"85",85)
AddComboValue(#Hertz,"100",100)

AddComboValue(#DeferredMSAALevel,"As Window",-1)
AddComboValue(#DeferredMSAALevel,"Off",0)
AddComboValue(#DeferredMSAALevel,"2",2)
AddComboValue(#DeferredMSAALevel,"4",4)
AddComboValue(#DeferredMSAALevel,"6",6)
AddComboValue(#DeferredMSAALevel,"8",8)
AddComboValue(#DeferredMSAALevel,"16",16)

AddComboValue(#OpenGLMode,"1.1",11)
AddComboValue(#OpenGLMode,"1.4",14)
AddComboValue(#OpenGLMode,"2.1",21)
AddComboValue(#OpenGLMode,"3.0",30)
AddComboValue(#OpenGLMode,"3.1",31)
AddComboValue(#OpenGLMode,"3.2",32)

AddComboValue(#OpenGLGLSLVersion,"120",120)
AddComboValue(#OpenGLGLSLVersion,"130",130)
AddComboValue(#OpenGLGLSLVersion,"140",140)
AddComboValue(#OpenGLGLSLVersion,"150",150)

LoadingWindowXY\X = -1
LoadingWindowXY\Y = -1

ProcessOptions(2,"")

Load()

Quit = 0

Repeat
	Event = WaitWindowEvent()
	
	Select(Event)
	Case #PB_Event_CloseWindow
		Quit = 1
		
	Case #PB_Event_Menu
		Select EventMenu()
			Case #MENU_3
				Quit = 1
				
			Case #MENU_5
				MessageRequester("About", "jEngine Config"+Chr(13)+"(c) jimon game studio", #PB_MessageRequester_Ok)
		EndSelect
	
	Case #PB_Event_Gadget
		Select EventGadget()
			Case #Save
				Save()
		EndSelect
		
	EndSelect
	
Until Quit = 1


; IDE Options = PureBasic 4.31 (Windows - x86)
; CursorPosition = 75
; FirstLine = 65
; Folding = --
; EnableXP
; UseIcon = advanced.ico
; Executable = Options.exe
; IncludeVersionInfo
; VersionField0 = 1,0,0,0
; VersionField1 = 1,0,0,0
; VersionField2 = jimon game studio
; VersionField3 = jEngine Config
; VersionField4 = 1,0
; VersionField5 = 1,0
; VersionField6 = jEngine Config
; VersionField7 = jEngine Config
; VersionField8 = Config.exe
; VersionField9 = (c) jimon game studio